﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using DungChung;
using System.Threading;
namespace CoVuaServer
{
    class DangNhapProcess
    {
        public List<QuanliKetNoi> QuanliKetNois; 
        private DataProvider dataProvider;
        public DangNhapProcess(string connectionString)
        {
            QuanliKetNois = new List<QuanliKetNoi>();
            dataProvider = new DataProvider(connectionString);
        }
        public void Start(object obj)
        {
            TaiKhoanDAO taiKhoanDao = new TaiKhoanDAO(dataProvider);
            TcpClient client = (TcpClient)obj;
            TaiKhoanDTO taiKhoan = new TaiKhoanDTO();
            NetworkStream stream = client.GetStream();
            BinaryReader reader = new BinaryReader(stream);
            BinaryWriter writer = new BinaryWriter(stream);
            taiKhoan.UserName = reader.ReadString();
            taiKhoan.Password = reader.ReadString();
            if (taiKhoanDao.GetAllTaiKhoan("select * from TAIKHOAN where Username = '" + taiKhoan.UserName +
                "' and Password = '" + taiKhoan.Password + "'").Count == 0)
            {
                writer.Write(MyTask.DangNhapThatBai);
            }
            else
            {
                if (CheckTaiKhoan(taiKhoan))
                {
                    writer.Write(MyTask.DangNhapThanhCong);
                    taiKhoan = taiKhoanDao.GetAllTaiKhoan("select * from TAIKHOAN where Username = '" + taiKhoan.UserName + "'")[0];
                    QuanliKetNoi ql = new QuanliKetNoi(client, taiKhoan);
                    ql.SendInfo();
                    ql.DeNghiNhanDanhSach();
                    ql.SendSoLuong(QuanliKetNois.Count);
                    foreach (var quanliKetNoi in QuanliKetNois)
                    {
                        ql.SendAnotherUser(quanliKetNoi.TaiKhoan);
                        quanliKetNoi.CoNguoiMoiDangNhap();
                        quanliKetNoi.SendAnotherUser(ql.TaiKhoan);
                    }
                    QuanliKetNois.Add(ql);
                    while (true)
                    {
                        string request = ql.ReadRequest();
                        if (request == MyTask.CLOSE)
                        {
                            #region Xu li Thoat Tai khoan
                            QuanliKetNois.Remove(ql);
                            ql.DongKetNoi();
                            Console.WriteLine(QuanliKetNois.Count.ToString());
                            foreach (var quanLiKetNoi in QuanliKetNois)
                            {
                                quanLiKetNoi.CoNguoiMoiDangXuat();
                                quanLiKetNoi.SendAnotherUser(ql.TaiKhoan);
                            }
                            break;
                            #endregion
                        }
                        if (request == MyTask.LoiMoiThamGiaVanDau)
                        {
                            #region Xu li loi moi tham gia van dau
                            string UserName = ql.ReadRequest();
                            foreach (var quanLiKetNoi in QuanliKetNois)
                            {
                                if (quanLiKetNoi.TaiKhoan.UserName == UserName)
                                {
                                    if (quanLiKetNoi.DangRanh)
                                    {
                                        quanLiKetNoi.GuiLoiMoi(ql.TaiKhoan);
                                    }
                                    else
                                    {
                                        ql.NguoiChoiDangBan(quanLiKetNoi.TaiKhoan.TenHienThi);
                                    }
                                }
                            }
                            #endregion
                        }
                        if (request == MyTask.TuChoi)
                        {
                            #region Xu li nguoi choi tu choi loi moi
                            string username = ql.ReadRequest();
                            foreach (var quanLiKetNoi in QuanliKetNois)
                            {
                                if (quanLiKetNoi.TaiKhoan.UserName == username)
                                {
                                    quanLiKetNoi.DaTuChoi();
                                }
                            }
                            #endregion
                        }
                        if (request == MyTask.NhanLoiMoi)
                        {
                            #region Xu li nguoi choi nhan loi moi
                            string username = ql.ReadRequest();
                            ql.DangRanh = false;
                            foreach (var quanLiKetNoi in QuanliKetNois)
                            {
                                if (quanLiKetNoi.TaiKhoan.UserName == username)
                                {
                                    quanLiKetNoi.NguoiCungChoi = ql;
                                    ql.NguoiCungChoi = quanLiKetNoi;
                                    quanLiKetNoi.DaNhanLoi(ql.TaiKhoan.UserName);
                                    quanLiKetNoi.DangRanh = false;
                                }
                                else
                                {
                                    if (ql.TaiKhoan.UserName != quanLiKetNoi.TaiKhoan.UserName)
                                    {
                                        quanLiKetNoi.CoNguoiTaoMoiTranDau(username, ql.TaiKhoan.UserName);
                                    }
                                }
                            }
                            #endregion
                        }
                        if (request == MyTask.Chat)
                        {
                            #region Xu li tin Chat
                            string message = ql.ReadRequest();
                            if (ql.NguoiCungChoi != null)
                            {
                                ql.NguoiCungChoi.SendMessageChat(ql.TaiKhoan.TenHienThi, message);
                            }
                            #endregion
                        }
                        if (request == MyTask.XemDanhSach)
                        {
                            #region Xu li yeu cau xem danh sach nguoi choi cao diem
                            if (ql.DangRanh = true)
                            {
                                ql.DangRanh = false;
                                TaiKhoanDTO[] dsNguoiChoiCaoDiem = new TaiKhoanDTO[20];
                                dsNguoiChoiCaoDiem = taiKhoanDao.Lay20NguoiDiemCao();
                                foreach (var tk in dsNguoiChoiCaoDiem)
                                {
                                    ql.SendAnotherUser(tk);
                                }
                                ql.DangRanh = true;
                            }
                            else
                            {
                                TaiKhoanDTO[] dsNguoiChoiCaoDiem = new TaiKhoanDTO[20];
                                dsNguoiChoiCaoDiem = taiKhoanDao.Lay20NguoiDiemCao();
                                foreach (var tk in dsNguoiChoiCaoDiem)
                                {
                                    ql.SendAnotherUser(tk);
                                }
                            }
                            #endregion
                        }
                        if (request == MyTask.XinThua)
                        {
                            #region Xu li yeu cau xin thua
                            if (ql.NguoiCungChoi != null)
                            {
                                ql.DangRanh = true;
                                ql.NguoiCungChoi.NguoiChoiXinThua();
                                ql.NguoiCungChoi.DangRanh = true;
                                GhiKetQuaVanDau(ref ql, 2, 1);
                            }
                            #endregion
                        }
                        if (request == MyTask.NuocDi)
                        {
                            ql.XuLyNuocDi();
                        }
                        if (request == MyTask.ChienThang)
                        {
                            ql.DangRanh = true;
                            ql.Writer.Write(ql.TaiKhoan.KinhNghiem);
                            ql.NguoiCungChoi.Writer.Write(MyTask.ChienThang);
                            ql.NguoiCungChoi.Writer.Write(ql.NguoiCungChoi.TaiKhoan.KinhNghiem);
                            ql.NguoiCungChoi.DangRanh = true;
                            GhiKetQuaVanDau(ref ql, 1, 2);
                        }
                    }   
                }
                else
                {
                    writer.Write(MyTask.TaiKhoanDaCoNguoiSuDung);
                }
            }
        }

        public bool CheckTaiKhoan(TaiKhoanDTO taiKhoan)
        {
            bool kiemTra = true;
            foreach (var quanliKetNoi in QuanliKetNois)
            {
                if (taiKhoan.UserName == quanliKetNoi.TaiKhoan.UserName)
                {
                    kiemTra = false;
                }
            }
            return kiemTra;
        }

        public void GhiKetQuaVanDau(ref QuanliKetNoi ketNoi, int ketQua, int ketQua2)
        {
            VanDauDAO vanDau = new VanDauDAO(dataProvider);
            VanDauDTO van = new VanDauDTO();
            LoaiKetQuaDAO loaiKetQua = new LoaiKetQuaDAO(dataProvider);
            van.NguoiChoi1 = ketNoi.TaiKhoan.UserName;
            van.NguoiChoi2 = ketNoi.NguoiCungChoi.TaiKhoan.UserName;
            van.KetQua = ketQua;
            van.MaVanDau = vanDau.GetAllVanDau("select * from VANDAU").Count + 1;
            van.ThoiGianTao = DateTime.Now;
            vanDau.ThemVanDauMoi(van);
            ketNoi.TaiKhoan.KinhNghiem += loaiKetQua.GetAllLoaiKetQua("select * from LOAIKETQUA where MaLoai = " + ketQua)[0].Diem;
            ketNoi.NguoiCungChoi.TaiKhoan.KinhNghiem += 
                loaiKetQua.GetAllLoaiKetQua("select * from LOAIKETQUA where MaLoai = " + ketQua2)[0].Diem;
            TaiKhoanDAO taiKhoan = new TaiKhoanDAO(dataProvider);
            taiKhoan.UpdateDiem(ketNoi.NguoiCungChoi.TaiKhoan);
            taiKhoan.UpdateDiem(ketNoi.TaiKhoan);
        }
    }
}
